<?php

namespace CMS;

class Route {
	private static $root;
	private static $uri;

	private static function reindex() {
		self::$uri = array_merge(self::$uri);
	}

	private static function clear() {
		foreach(self::$uri as $idx => $item)
			if(empty($item))
				unset(self::$uri[$idx]);

		self::reindex();
	}

	private static function bench() {
		self::$uri = array_diff(self::$uri, self::$root);

		self::reindex();
	}

	private static function execute() {
		$ctrl = array_shift(self::$uri);

		$app = new App(self::$uri);
		$app->run($ctrl);
	}

	public static function init() {
		self::$root = array_reverse(explode(DS, ROOT));
		self::$uri = explode('/', preg_replace('/^([^\?]*).*$/', '$1', $_SERVER['REQUEST_URI']));

		self::clear();
		self::bench();
		self::execute();
	}
}